package com.dysoft.flesh.gl;

import com.dysoft.flesh.RenderComponent;
import com.dysoft.flesh.RenderContext;

import javax.media.opengl.GL;
import javax.media.opengl.GLContext;
import javax.media.opengl.glu.GLU;
import javax.vecmath.Vector3f;

/**
 * @author Sean Micklethwaite
 *         21-Aug-2010
 */
public class GLCamera implements RenderComponent {
	Vector3f position, direction, up;
	final GLU glu = new GLU();

	public GLCamera() {
	}

	public GLCamera(Vector3f position, Vector3f direction, Vector3f up) {
		this.position = position;
		this.direction = direction;
		this.up = up;
	}

	public void activate(RenderContext ctx) {
		GL gl = GLContext.getCurrent().getGL();

		float ratio = (float)ctx.getTarget().getWidth() / (float)ctx.getTarget().getHeight();
		gl.glMatrixMode(GL.GL_PROJECTION);
		gl.glPushMatrix();
		gl.glLoadIdentity();
		glu.gluPerspective(45f, ratio, 0.1, 200);
		glu.gluLookAt(position.x, position.y, position.z,
				direction.x, direction.y, direction.z, up.x, up.y, up.z);
		gl.glMatrixMode(GL.GL_MODELVIEW);
	}

	public void deactivate(RenderContext ctx) {
		GL gl = GLContext.getCurrent().getGL();
		gl.glMatrixMode(GL.GL_PROJECTION);
		gl.glPopMatrix();
		gl.glMatrixMode(GL.GL_MODELVIEW);
	}

	public Vector3f getPosition() {
		return position;
	}

	public void setPosition(Vector3f position) {
		this.position = position;
	}

	public Vector3f getDirection() {
		return direction;
	}

	public void setDirection(Vector3f direction) {
		this.direction = direction;
	}

	public Vector3f getUp() {
		return up;
	}

	public void setUp(Vector3f up) {
		this.up = up;
	}
}
